<?php

/**
 * Check authentication
 */
class Auth extends SM_ADMIN_Controller
{

    private $_data;

    /**
     * Initialization
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Show the login form
     */
    public function login()
    {
        $this->load->helper('auth');
        $this->load->library('form_validation');
        $this->form_validation->set_rules(loginFieldValidate());
        if ($this->form_validation->run()) {
            $form = $this->input->post();
            $this->load->model('users_model');
            $user = $this->users_model->checkUsername($form['username']);
            if ($this->smauth->authenticate($form['username'], $form['password'], $user)) {
                redirect('/admin/dashboard/');
            } else {
                $this->_data['error'] = 'Username or password is wrong!';
            }
        }
        $this->_data['form'] = loginFieldForm();
        $this->load->view('layout/login', array('view' => 'auth/login', 'data' => $this->_data));
    }

    /**
     * Logout action
     */
    public function logout()
    {
        $this->smauth->logout();
        redirect('/admin/auth/login/');
    }

}
